'use strict';
angular.module('app')
  .directive('signAlert', function () {
    return {
      restrict: 'E',
      scope: {
        showSignAlert: '=showSign',
        signPoint: '='
      },
      transclude: true,
      templateUrl: './src/app/sign/signAlert.html?v=' + v,
      controller: ['$scope', 'ajaxRquest', '$rootScope', '$state', '$interval', '$location', function ($scope, ajaxRquest, $rootScope, $state, $interval, $location) {
        $scope.closeAlert = function () { //关闭弹窗
          $scope.showSignAlert = false;
          document.removeEventListener('touchmove', touchmove, false)
        }
        document.addEventListener('touchmove', touchmove, false)
        function touchmove(e) {
          preventDefault(e)
        }
        function preventDefault(e) {
          if (e && e.preventDefault) {
            e.preventDefault()
          } else {
            window.event.returnValue = false
          }
        }
      }]
    }
  })
  .controller('signController', ['$rootScope', '$scope', 'ajaxRquest', '$state', '$location', function ($rootScope, $scope, ajaxRquest, $state, $location) {
    $scope.showSignAlert = false //弹窗默认关闭
    // $scope.flag = false; // 判断是否签到的标志，从后台获取  
    var array = [];
    $scope.url = $location.search()
    $scope.token = $scope.url['token'];
    $.cookie("huiToken", $scope.token)
    // $.cookie("huiToken", "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMDEwMDc3IiwiZXhwIjoxNTE1NjU0NDUxLCJpYXQiOjE1MTU2NDg0NTF9.vV8gyiWaAEIHNrN3N8BK7ldPcbbO_K84lWcbI6fHTuw")
    function getSign() {
      ajaxRquest.getData('/signModule/signHistory', {}).then(function (res) {
        console.log(res)
        if (res.resCode == '00000') {
          $scope.$apply(function () {
            if (res.record.isSign == 'Y') {
              $scope.flag = true; // 判断是否签到的标志 ，'Y'表示已签到    
            } else {
              $scope.flag = false; // 判断是否签到的标志，'N'表示未签到                
            }
            $scope.signPoint = res.record.signPoint
            $scope.signDays = res.record.signDays
            $scope.initDayArrays(res.record.nowDate, res.records)
          })
        } else {
          if (res.resCode == '00003') {
            $.cookie("huiToken", null)
            $scope.formError({
              text: res.resMsg, //内容
              resCode: res.resCode
            })
          }
        }
      })

    }
    getSign()
    $scope.signAlert = function () {
      $scope.showSignAlert = true; // 打开弹窗
      $scope.flag = true; //显示已签到
      ajaxRquest.getData('/signModule/sign', {}).then(function (res) {
        console.log(res)
        if (res.resCode == '00000') {
          $scope.$apply(function () {
            $scope.signDays = res.record.signDays
            getSign()
          })
        } else {
          if (res.resCode == '00003') {
            $.cookie("huiToken", null)
            $scope.formError({
              text: res.resMsg, //内容
              resCode: res.resCode
            })
          }
        }
      })
    }
    $scope.initDayArrays = function (time, value) {
      var dates = []; // 存储已签到日期,积分
      var year = time.split('-')[0] // 取年份
      $scope.month = time.split('-')[1] // 取月份 
      // console.log(year, $scope.month)
      value.forEach(function (v, i) {
        var date = { 'signDate': parseInt(v.signDate.split('-')[2]), 'signDesc': v.signDesc, 'signPoint': v.signPoint }
        dates.push(date);
      })

      // var date = new Date(2017, 10, 1);
      // $scope.month = date.getMonth();      
      // var date = new Date();
      // $scope.month = date.getMonth() + 1;      
      // var year = date.getFullYear()
      // var getdate = date.getDate();
      // console.log(getdate)

      //获取月份天数
      function getMonthDays(year, month) {
        return new Date(year, month, 0).getDate();
      }

      //获取星期几
      function getWeekday(year, month, day) {
        return new Date(year, month, day).getDay();
      }
      $scope.weekTable = ['日', '一', '二', '三', '四', '五', '六']
      var totalDay = getMonthDays(year, $scope.month) //当前月份天数    
      var day = getWeekday(year, $scope.month - 1, 1) //1号星期几
      $scope.dayArrays = new Array() //签到信息列表
      for (var i = 0; i < day; i++) {
        $scope.dayArrays.push({ "signed": false, "date": '', "integral": '', "weekDay": '' }); //1号之前$scope.dayArrays.date为空
      }

      for (var i = 1; i <= totalDay; i++) {
        var mark = false //标记当月是否有签到
        var weekDayFlag = (day + i - 1) % 7;// 计算出当前日期对应的星期几
        if (dates.length != 0) { //当月签过到
          for (var j = 0; j < dates.length; j++) {
            if (dates[j]['signDate'] == i) { //表示已签到
              mark = true;
              $scope.dayArrays.push({ "signed": true, "date": i, "integral": dates[j]['signPoint'], "weekDay": $scope.weekTable[weekDayFlag] })
              break // 结束本次循环
            }
            if (j == dates.length - 1 && !mark) { //当前日期没有签到
              $scope.dayArrays.push({ "signed": false, "date": i, "integral": '', "weekDay": $scope.weekTable[weekDayFlag] })
              break // 结束本次循环
            }
          }
        } else { //当月没有签过到
          $scope.dayArrays.push({ "signed": false, "date": i, "integral": '', "weekDay": $scope.weekTable[weekDayFlag] })
        }

      }
      // console.log($scope.dayArrays)
      // $scope.dayArrays[6 + day] = { "signed": true, "date": 6, "integral": '2', "weekDay": '三' }
      // $scope.dayArrays[7 + day] = { "signed": true, "date": 7, "integral": '3', "weekDay": '四' }
    }

  }]);
